﻿function wrapString(str, fr, bck) {
    if (bck == null)
        bck = fr;

    return fr + str + bck;
}

function toDotNetParam(obj) {
    if (obj == null)
        return "{}";

    var output = "";

    for (var iter in obj)
        output += wrapString(iter, "\"", null) + ":" + wrapString(obj[iter], "'") + ", ";

    return wrapString(output.substring(0,output.length - 2), "{", "}");
}

function ajax(url, params, onSucceeded, onError) {
    var settings = {};

    settings.type = "POST";
    settings.url = url;
    settings.data = toDotNetParam(params);
    settings.contentType = "application/json; charset=utf-8";
    settings.dataType = "json";
    settings.success = onSucceeded;
    settings.error = onError;

    $.ajax(settings);
}

function messageBox(title, message, onClose) {
    $("body").append(
                "<div id='alertBox' class='modal fade'>" +
                    "<div class='modal-header'>" +
                        "<a class='close' data-dismiss='modal'>&times;</a>" +
                        "<h3>" + title + "</h3>" +
                    "</div>" +
                    "<div class='modal-body'>" +
                    "<p>" + message + "</p>" +
                    "</div>" +
                    "<div class='modal-footer'>" +
                    "<a data-dismiss='modal' href='#' class='btn btn-primary'>Okay</a>" +
                    "</div>" +
                "</div>");
    $("#alertBox").on('hidden', function () {
        $("#alertBox").remove();

        if (onClose != null)
            onClose();
    });

    $("#alertBox").modal({ backdrop: "static" });
    $("#alertBox").modal("show");
}

function confirmBox(title, message, onClose) {
    $("body").append(
                "<div id='confirmBox' class='modal fade'>" +
                    "<div class='modal-header'>" +
                        "<a class='close' data-dismiss='modal'>&times;</a>" +
                        "<h3>" + title + "</h3>" +
                    "</div>" +
                    "<div class='modal-body'>" +
                    "<p>" + message + "</p>" +
                    "</div>" +
                    "<div class='modal-footer'>" +
                    "<a data-dismiss='modal' action='no' href='#' class='btn btn-danger'>No</a>" +
                    "<a data-dismiss='modal' action='yes' href='#' class='btn btn-success'>Yes</a>" +
                    "</div>" +
                "</div>");

    $("#confirmBox div.modal-footer a").click(function (ev) {
        if (onClose != null)
            onClose($(this).attr("action") == "yes");
    });

    $("#confirmBox").on('hidden', function () {
        $("#confirmBox").remove();
    });

    $("#confirmBox").modal({ backdrop: "static" });
    $("#confirmBox").modal("show");
}

function inputBox(title, message, inputs, onSubmit) {
    $("body").append(
                "<div id='inputBox' class='modal fade'>" +
                    "<div class='modal-header'>" +
                        "<a class='close' data-dismiss='modal'>&times;</a>" +
                        "<h3>" + title + "</h3>" +
                    "</div>" +
                    "<div class='modal-body'>" +
                    "<p>" + message + "</p>" +
                    "<div id='inputBox-formContent'><form id='inputBox-form'></form></div>" +
                    "</div>" +
                    "<div class='modal-footer'>" +
                    "<a data-dismiss='modal' action='close' href='#' class='btn btn-danger'>Close</a>" +
                    "<a action='done' href='#' class='btn btn-success'>Done!</a>" +
                    "</div>" +
                "</div>");

    var formContent = "";

    for (var i in inputs) {
        var input = inputs[i];

        switch (input.type) {
            case "text":
                formContent += "<label for='" + input.id + "'>" + input.name +
                    "</label><input type='text' class='input-xlarge " + input.validation +
                    "' id='" + input.id + "' name='" + input.id + "' placeholder='" + 
                    input.placeholder + "'/>";
                break;

            case "select":
                var selectContent = "";
                for (var j in input.items) {
                    var item = input.items[j];
                    selectContent += "<option value='" + item.id + "'" + (item.selected ? "selected='selected'" : "")
                    + ">" + item.desc + "</option>";
                }

                selectContent = wrapString(selectContent, "<select id='" + input.id + "' name='" + input.id
                + "'>", "</select>");
                formContent += "<label for='" + input.id + "'>" + input.name + "</label>" + selectContent;
                break;

            default:
                break;
        }
    }

    $("#inputBox-form").append(formContent);

    $("#inputBox div.modal-footer a").click(function (ev) {
        if ($(this).attr("action") == "done") {
            ev.preventDefault();
            var val = $("#inputBox-form").validationEngine("validate");

            if (val) {
                $("#inputBox").modal("hide");

                if (onSubmit != null) {
                    for (var i in inputs) {
                        var input = inputs[i];

                        switch (input.type) {
                            case "text":
                                input.value = $("#" + input.id).val();
                                break;
                            case "select":
                                input.value = $("#" + input.id + " option:selected").val();
                                break;

                            default:
                                break;
                        }
                    }

                    onSubmit(true, inputs);
                }
            }

            return false;
        }
    });

    $("#inputBox").on('hidden', function () {
        $("#inputBox-form").validationEngine("detach");
        $("#inputBox").remove();
    });

    $("#inputBox").modal({ backdrop: "static" });
    $("#inputBox").modal("show");
    $("#inputBox-form").validationEngine('attach', { scroll: false, autoHidePrompt: true });
}